<?php
class MeetingHelper
{
	const SQL = 'SELECT MAX(meeting_date) m FROM `meetings` WHERE project_id = :project_id';

	public static function isLastMeeting($meeting)
	{
		$cmd = Yii::app()->db->createCommand(self::SQL);
		$date = $cmd->queryScalar(array(':project_id' => $meeting->project_id));
		
		return $date === $meeting->meeting_date;
	}

	public static function canAccessMeeting ($meeting)
	{
		if (Yii::app()->user->isGuest)
		{
			return false;
		}

		if (ProjectHelper::canReadProject($meeting->project))
		{
			return true;
		}

		return false;
	}

	public static function canEditMeeting ($meeting)
	{
		if (! self::canAccessMeeting($meeting))
		{
			return false;
		}
		
		if (! self::isLastMeeting($meeting))
		{
			return false;
		}
		
		if ($meeting->completed)
		{
			return false;
		}
		
		if (ProjectHelper::canEditProject($meeting->project))
		{
			return true;
		}
		
		return false;
	}
}